//
//  FormalThreeViewController.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/5.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "FormalThreeViewController.h"
#import "FormalTabBar.h"
#import "FormalThreeHeaderView.h"
#import "FangdaiTableViewCell.h"
#import "GerendaikuanTableViewCell.h"
#import "ChedaiTableViewCell.h"
#import "CunchuTableViewCell.h"
#import "XinyongkaTableViewCell.h"
#import "OtherTableViewCell.h"
#import "LoadConfig.h"

@interface FormalThreeViewController (){
    FormalTabBar                 *tabBarView;
    BOOL                              isFangdai;
    BOOL                              isChedai;
    BOOL                              isGerendaikuan;
    BOOL                              isCunchu;
    BOOL                              isXinyongka;
    BOOL                              isOther;
    NSDictionary                      *toolsConfig;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation FormalThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"助手";
    tabBarView = [FormalTabBar sharedFormalTabBar];
    [self.navigationItem setHidesBackButton:YES];
    
    FormalThreeHeaderView *headerView = [[FormalThreeHeaderView alloc] initItem];
    self.tableView.tableHeaderView = headerView;
    
    [self loadingHUDAlert:@"正在加载"];
    NSDictionary *configDic = [[LoadConfig shareStance] getTools];
    if (configDic.count > 0) {
        toolsConfig = configDic;
        [headerView setContentConfig:toolsConfig];
        [self hideHudAlert];
    }
    [[LoadConfig shareStance] loadToolsSuccess:^(NSDictionary *dic) {
        toolsConfig = dic;
        [headerView setContentConfig:toolsConfig];
        [self hideHudAlert];
    }];
    
    [self.tableView setFrame:CGRectMake(0, 64 + IS_iPhoneX_Top, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Top - 64 - 50 - IS_iPhoneX_Bottom)];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.fd_interactivePopDisabled = YES;
    [self.view addSubview:tabBarView];
    [tabBarView setCurrentViewControllerIndex:3];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.fd_interactivePopDisabled = NO;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return 80;
    }
    return 160;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        if (isFangdai) {
            return 1;
        }else{
            return 0;
        }
    }else if (section == 1){
        if (isChedai) {
            return 1;
        }else{
            return 0;
        }
    }else if (section == 2){
        if (isGerendaikuan) {
            return 1;
        }else{
            return 0;
        }
    }else if (section == 3){
        if (isCunchu) {
            return 1;
        }else{
            return 0;
        }
    }
    //    else if (section == 4){
    //        if (isXinyongka) {
    //            return 1;
    //        }else{
    //            return 0;
    //        }
    //    }
    else if (section == 4){
        if (isOther) {
            return 1;
        }else{
            return 0;
        }
    }
    return 0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 40)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 40)];
    titleLabel.textColor = TextColor;
    titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [view addSubview:titleLabel];
    
    UIImageView *rightIcon = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH_DEVICE - 26, 15, 16, 9)];
    [view addSubview:rightIcon];
    
    if (section == 0) {
        titleLabel.text = @"房贷计算器";
        UIButton *actionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 40)];
        [view addSubview:actionBtn];
        [actionBtn addTarget:self action:@selector(fangdaiPress) forControlEvents:UIControlEventTouchUpInside];
        if (isFangdai) {
            [rightIcon setImage:[UIImage imageNamed:@"up"]];
        }else{
            [rightIcon setImage:[UIImage imageNamed:@"down"]];
        }
    }else if (section == 1){
        titleLabel.text = @"车贷计算器";
        UIButton *actionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 40)];
        [view addSubview:actionBtn];
        [actionBtn addTarget:self action:@selector(chedaiPress) forControlEvents:UIControlEventTouchUpInside];
        if (isChedai) {
            [rightIcon setImage:[UIImage imageNamed:@"up"]];
        }else{
            [rightIcon setImage:[UIImage imageNamed:@"down"]];
        }
    }else if (section == 2){
        titleLabel.text = @"个人贷款计算器";
        UIButton *actionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 40)];
        [view addSubview:actionBtn];
        [actionBtn addTarget:self action:@selector(gerendaikuanPress) forControlEvents:UIControlEventTouchUpInside];
        if (isGerendaikuan) {
            [rightIcon setImage:[UIImage imageNamed:@"up"]];
        }else{
            [rightIcon setImage:[UIImage imageNamed:@"down"]];
        }
    }else if (section == 3){
        titleLabel.text = @"存储计算器";
        UIButton *actionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 40)];
        [view addSubview:actionBtn];
        [actionBtn addTarget:self action:@selector(cunchuPress) forControlEvents:UIControlEventTouchUpInside];
        if (isCunchu) {
            [rightIcon setImage:[UIImage imageNamed:@"up"]];
        }else{
            [rightIcon setImage:[UIImage imageNamed:@"down"]];
        }
    }
    //    else if (section == 4){
    //        titleLabel.text = @"信用卡计算器";
    //        UIButton *actionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 40)];
    //        [view addSubview:actionBtn];
    //        [actionBtn addTarget:self action:@selector(xinyongkaPress) forControlEvents:UIControlEventTouchUpInside];
    //        if (isXinyongka) {
    //            [rightIcon setImage:[UIImage imageNamed:@"up"]];
    //        }else{
    //            [rightIcon setImage:[UIImage imageNamed:@"down"]];
    //        }
    //    }
    else if (section == 4){
        titleLabel.text = @"其他计算器";
        UIButton *actionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 40)];
        [view addSubview:actionBtn];
        [actionBtn addTarget:self action:@selector(otherPress) forControlEvents:UIControlEventTouchUpInside];
        if (isOther) {
            [rightIcon setImage:[UIImage imageNamed:@"up"]];
        }else{
            [rightIcon setImage:[UIImage imageNamed:@"down"]];
        }
    }
    
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 39, SCREEN_WIDTH_DEVICE - 15, 1)];
    lineImage.image = [UIImage imageNamed:@"lineBottom"];
    [view addSubview:lineImage];
    
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *cellIdentifier = @"FangdaiCell";
        FangdaiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"FangdaiTableViewCell" owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setContentConfig:toolsConfig];
        return cell;
    }else if (indexPath.section == 1){
        static NSString *cellIdentifier = @"ChedaiCell";
        ChedaiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ChedaiTableViewCell" owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setContentConfig:toolsConfig];
        return cell;
    }else if (indexPath.section == 2){
        static NSString *cellIdentifier = @"GerendaikuanCell";
        GerendaikuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"GerendaikuanTableViewCell" owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setContentConfig:toolsConfig];
        return cell;
    }else if (indexPath.section == 3){
        static NSString *cellIdentifier = @"CunchuCell";
        CunchuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CunchuTableViewCell" owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setContentConfig:toolsConfig];
        return cell;
    }
    //    else if (indexPath.section == 4){
    //        static NSString *cellIdentifier = @"XinyongkaCell";
    //        XinyongkaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //        if (!cell) {
    //            cell = [[[NSBundle mainBundle] loadNibNamed:@"XinyongkaTableViewCell" owner:self options:nil] lastObject];
    //            cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //        }
    //        return cell;
    //    }
    else if (indexPath.section == 4){
        static NSString *cellIdentifier = @"OtherCell";
        OtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"OtherTableViewCell" owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setContentConfig:toolsConfig];
        return cell;
    }
    
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}

- (void)fangdaiPress{
    isFangdai = !isFangdai;
    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)chedaiPress{
    isChedai = !isChedai;
    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)gerendaikuanPress{
    isGerendaikuan = !isGerendaikuan;
    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:2] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)cunchuPress{
    isCunchu = !isCunchu;
    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:3] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)xinyongkaPress{
    isXinyongka = !isXinyongka;
    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:4] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)otherPress{
    isOther = !isOther;
    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:4] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
